package com.iceframework.core.entity;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
 * 通用返回结果类 可链式操作
 * 
 * @author Palo
 *
 */
@JsonInclude(value = Include.NON_NULL)
public class Result {

	public final static String SUCCESS = "200";
	public final static String FAIL = "304";
	public final static String EXCEPTION = "500";
	public final static String UNAUTHORIZED = "401";
	
	public boolean success;
	//内部状态码
	private String code;
	// 提示信息
	private String msg;

	// 用户要返回浏览器的数据
	private Map<String, Object> data = new HashMap<String, Object>();

	public Result() {
	}

	public static Result newOne() {
		return new Result();
	}

	public Result add(String key, Object value) {
		this.data.put(key, value);
		return this;
	}

	public static Result success() {
		Result result = new Result();
		result.setCode(SUCCESS);
		result.setMsg("操作成功");
		result.success=true;
		return result;
	}

	public static Result fail() {
		Result result = new Result();
		result.setCode(FAIL);
		result.setMsg("操作失败");
		result.success=false;
		return result;
	}

	public static Result delSuccess() {
		Result result = new Result();
		result.setCode(SUCCESS);
		result.setMsg("删除成功");
		result.success=true;
		return result;
	}

	public static Result delFail() {
		Result result = new Result();
		result.setCode(FAIL);
		result.setMsg("删除失败");
		result.success=false;
		return result;
	}

	public static Result saveSuccess() {
		Result result = new Result();
		result.setCode(SUCCESS);
		result.setMsg("保存成功");
		result.success=true;
		return result;
	}

	public static Result saveFail() {
		Result result = new Result();
		result.setCode(FAIL);
		result.setMsg("保存失败");
		result.success=false;
		return result;
	}

	public static Result exception() {
		Result result = new Result();
		result.setCode(EXCEPTION);
		result.setMsg("操作失败");
		result.success=false;
		return result;
	}

	public Result setMsg(String msg) {
		this.msg = msg;
		return this;
	}
	
	public String getMsg() {
		return msg;
	}

	public String getCode() {
		return code;
	}

	public Result setCode(String code) {
		this.code = code;
		return this;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public Map<String, Object> getExtends() {
		return data;
	}

}
